local skill = fk.CreateSkill{
  name = "tea__tengchao",
}
local U = require "packages/utility/utility"
---@type ViewAsSkillSpec
local tengchao = {
  anim_type = "special",
  pattern = ".|.|.|.|.|trick",
  prompt = "#tea__tengchao_other-active",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("t")
    local names = player:getViewAsCardNames("tea__tengchao", all_names, player:getCardIds("he"))
    local use_names = table.map(player:getPile("tea__zhuanfu_record"), function(c)
      return Fk:getCardById(c).name
    end)
    names = table.filter(names, function(n)
      return table.contains(use_names, n)
    end)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    return true
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data or #cards <= Self:getMark("@tea__tengchao_num") then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skill.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    use.extra_data = {tengchao_use = true}
    room:setPlayerMark(player, "@tea__tengchao_num", #(use.card.subcards or {}))
  end,
  enabled_at_play = function(self, player)
    return player.phase ~= Player.NotActive and player:getMark("@tea__tengchao_num") > 0 and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player.phase ~= Player.NotActive and player:getMark("@tea__tengchao_num") > 0 and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
}
---@type TrigSkelSpec<TrigFunc>
local tengchao_trigger = {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == Fk.skills[skill.name]
    elseif event == fk.BuryVictim then
      return target:hasSkill(skill.name, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    else
      return player:hasSkill("tea__tengchao") and (data.extra_data or {}).tengchao_use
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event ~= fk.CardUseFinished then
      local suc = table.find(room.alive_players, function (p)
        return p ~= player and p:hasSkill(skill.name, true)
      end)
      if suc and not player:hasSkill("tea__tengchao&", true, true) then
        room:handleAddLoseSkills(player, "tea__tengchao&", nil, false, true)
      elseif not suc and player:hasSkill("tea__tengchao&", true, true) then
        room:handleAddLoseSkills(player, "-tea__tengchao&", nil, false, true)
      end
    else
      local cid = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        skill_name = skill.name,
        prompt = "#tea__tengchao_choose",
        pattern = tostring(Exppattern{ id = data.card.subcards }),
        expand_pile = data.card.subcards,
      })
      if #cid == 1 then
        room:obtainCard(player, cid, true, fk.ReasonPrey)
      end
    end
  end,
}
skill:addEffect("viewas", tengchao)
skill:addEffect(fk.EventAcquireSkill, tengchao_trigger)
skill:addEffect(fk.EventLoseSkill, tengchao_trigger)
skill:addEffect(fk.BuryVictim, tengchao_trigger)
skill:addEffect(fk.AfterPropertyChange, tengchao_trigger)
skill:addEffect(fk.CardUseFinished, tengchao_trigger)
return skill